package com.yanqu.road.server.cmd.activity.bowart;

import com.yanqu.road.entity.activity.bowart.BowArtUserData;
import com.yanqu.road.pb.activity.BowArtProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.bowart.BowArtModule;
import com.yanqu.road.server.manger.activity.bowart.BowArtMgr;
import com.yanqu.road.server.manger.activity.bowart.Coordinate;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = GameProtocol.S_BOW_ART_ACTIVITY_LAUNCH_ARROW, desc = "射箭")
public class BowArtLaunchArrowCmd extends AbstractCommand {

    int goodsId;
    int timeGap;
    Coordinate hitPoint;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        BowArtProto.BowArtLaunchArrowReqMsg reqMsg = BowArtProto.BowArtLaunchArrowReqMsg.parseFrom(packet.getBytes());
        goodsId = reqMsg.getGoodsId();
        timeGap = reqMsg.getTimeGap();
        hitPoint = new Coordinate(reqMsg.getPoint().getX(), reqMsg.getPoint().getY());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(BowArtMgr.getShowTimeActivityId() == 0){
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        if(!BowArtMgr.activityInTime()){
            ret = GameErrorCode.E_BOW_ART_ACTIVITY_NO_IN_TIME;
            return;
        }
        //判定水果道具是否有效
        if(goodsId != BowArtMgr.BOW_ART_MU_JIAN_ID && goodsId != BowArtMgr.BOW_ART_TIE_JIAN_ID &&
                goodsId != BowArtMgr.BOW_ART_LIU_YE_JIAN_ID && goodsId != BowArtMgr.BOW_ART_JIANG_JUN_JIAN_ID &&
                goodsId != BowArtMgr.BOW_ART_XUAN_YUAN_JIAN_ID){
            ret = GameErrorCode.E_COMMON_ACTIVITY_NO_ACTIVITY_GOODS;
            return;
        }
        ret = player.getModule(BowArtModule.class).launchArrow(goodsId, timeGap, hitPoint);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            BowArtProto.BowArtLaunchArrowRespMsg.Builder respMsg = BowArtProto.BowArtLaunchArrowRespMsg.newBuilder();
            respMsg.setRet(ret);
            BowArtUserData userData = player.getModule(BowArtModule.class).getUserData();
            respMsg.setUserData(BowArtMgr.getUserDataBuilder(userData));
            player.sendPacket(Protocol.U_BOW_ART_ACTIVITY_LAUNCH_ARROW, respMsg);
        }
    }
}
